package sf.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import sf.model.SQLTableModel;
import sf.view.StandardForm;

public class ActionPotvrdaUnosa extends SFAbstractAction {

	private static final long serialVersionUID = 1L;

	private StandardForm parentForm;

	ActionPotvrdaUnosa(StandardForm parentForm) {
		this.parentForm = parentForm;
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		putValue(MNEMONIC_KEY, KeyEvent.VK_O);
		putValue(SMALL_ICON, loadIcon("img/ok.png"));
		putValue(NAME, "OK");
		putValue(SHORT_DESCRIPTION, "Potvrdi unos");
	}

	public void actionPerformed(ActionEvent e) {
		addRow();
	}

	private void addRow() {
		if (parentForm.getDetailsPane().checkNullable()) {
			SQLTableModel tableModel = (SQLTableModel) parentForm.getBrowser().getTable().getModel();
			if (tableModel.addNewRow(parentForm.getDetailsPane().getFieldsAndValues())) {
				parentForm.getBrowser().selectLastRow();
				parentForm.getDetailsPane().clearAllFields();
				parentForm.getDetailsPane().focusFirstField();
			}
			else
				JOptionPane.showMessageDialog(parentForm, "<html><b>Unos neuspesan.</b></html>", "Greska", JOptionPane.ERROR_MESSAGE);
		}
		else
			JOptionPane.showMessageDialog(parentForm, "Niste popunili sva obavezna polja.", "Napomena", JOptionPane.WARNING_MESSAGE);
	}
}
